package com.ymtms.customerservice.dao;

import com.ymtms.customerservice.pojo.CustomerInformation;
import com.ymtms.customerservice.pojo.TotalFreight;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

public interface CustomerInformationMapper {
    int deleteByPrimaryKey(Integer ciid);

    int insert(CustomerInformation record);

    int insertSelective(CustomerInformation record);

    CustomerInformation selectByPrimaryKey(Integer ciid);

    int updateByPrimaryKeySelective(CustomerInformation record);

    int updateByPrimaryKey(CustomerInformation record);

    /**
     * 插入客户
     * @param customerInformation
     */
    @Insert("INSERT INTO customer_information(customer_code,customer,phone,fax,address,post_code," +
                "lin_kman,linkman_mobile,customer_type,enterprise_property,enterprise_size,email) " +
            "values(#{customerCode},#{customer},#{phone},#{fax},#{address},#{postCode},#{linKman},#{linkmanMobile}," +
                "#{customerType},#{enterpriseProperty},#{enterpriseSize},#{email})")
    void insertCustomerInformation(CustomerInformation customerInformation);

    @Select("SELECT send_goods_customer,sum(carriage) AS money FROM goodsbill_master WHERE flag=0 GROUP BY send_goods_customer ORDER BY money DESC")
    List<String> selectAllCarriageRankingCustomer();

    @Select("SELECT send_goods_customer AS name,sum(carriage) AS money FROM goodsbill_master WHERE flag=0 GROUP BY send_goods_customer ORDER BY money DESC")
    List<BigDecimal> selectAllCarriageRankingMoney();

    @Select("SELECT send_goods_customer AS name,sum(carriage) AS carriage,sum(reduce_fund) AS reduce_fund,sum(pay_kickback) AS pay_kickback," +
                "sum(money_of_change_pay) AS money_of_change_pay,sum(carry_goods_fee) AS carry_goods_fee,sum(insurance) AS insurance " +
                "FROM goodsbill_master WHERE flag=0 GROUP BY send_goods_customer ORDER BY carriage DESC")
    List<TotalFreight> selectAllCarriageRanking();


    List<CustomerInformation> selectCustomer(CustomerInformation customerInformation);

    /**
     * 查找客户编号
     * @return
     */
    @Select("SELECT customer_code FROM customer_information WHERE flag=0")
    List<String> getCustomercode();

    /**
     * 删除用户
     * @param customerCode
     */
    @Update("UPDATE customer_information SET flag=1 WHERE customer_code=#{customerCode}")
    void updateCustomerByCustomerCode(String customerCode);

    List<CustomerInformation> queryByPage(CustomerInformation complaintInformation);

    void customerModify(@Param("customer") CustomerInformation customerInformation);

    /**
     * 查客户编号
     * @param phone
     * @return
     */
    @Select("SELECT customer_code FROM customer_information WHERE phone=#{phone}")
    String selectCustomerCode(String phone);

    /**
     * 查客户名
     * @param phone
     * @return
     */
    @Select("SELECT customer FROM customer_information WHERE phone=#{phone}")
    String selectName(String phone);

    /**
     * 查管理员姓名
     * @param account
     * @return
     */
    @Select("SELECT employee_name FROM emp_information WHERE eiid=" +
            "(SELECT employee_pkid FROM user_emp WHERE user_pkid =" +
            "(SELECT uid FROM user WHERE login_id=#{account}) )")
    String selectUserName(String account);
}